package com.coder4j.service;

import com.coder4j.entity.SControlEntity;
import com.coder4j.entity.SDisplayInterfaceEntity;
import com.coder4j.mapper.SControlMapper;
import com.coder4j.utils.ListUtils;
import com.coder4j.vo.MenuData;
import com.coder4j.vo.SControlQuery;
import com.coder4j.vo.SControlVO;
import com.coder4j.starter.web.BaseServiceImpl;
import com.coder4j.vo.SDisplayInterfaceQuery;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;

import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 任务流程表
 * @author keller
 * @date 2025-03-18 16:22:15
 */
@Service
@Slf4j
public class SControlService extends BaseServiceImpl<SControlMapper,SControlEntity,SControlVO> {

    @Resource
    private SControlMapper mapper;

    @Override
    public SControlVO entity2Vo(SControlEntity entity) {
        SControlVO vo = new SControlVO();
        BeanUtils.copyProperties(entity,vo);
        return vo;
    }

    @Override
    public SControlEntity vo2Entity(SControlVO vo) {
        SControlEntity entity = new SControlEntity();
        BeanUtils.copyProperties(vo, entity);
        return entity;
    }

    public List<MenuData> getMenuData(){
        List<MenuData> list = new ArrayList<>();
        SControlQuery query = new SControlQuery();
        List<SControlEntity> entityList = this.baseQueryList(query);
        if(ListUtils.isEmpty(entityList)){
            return list;
        }

        Map<String, List<SControlEntity>> map = entityList.stream().collect(Collectors.groupingBy(SControlEntity::getControlType));
        for (String type : map.keySet()) {
            MenuData data = new MenuData();
            data.setKey(type);
            data.setLabel(type);
            data.setChildren(new ArrayList<>());
            for (SControlEntity entity : map.get(type)) {
                MenuData child = new MenuData();
                child.setKey(entity.getId()+"");
                child.setLabel(entity.getControlName());
                data.getChildren().add(child);
            }
            list.add(data);
        }
        return list;
    }

    public void putTaskId(Integer taskFlowId, Integer controlId) {
        mapper.putTaskId(taskFlowId,controlId);
    }
}
